<?php 

Class wechatModel extends appModel
{
	/**
	 * 关键字回复
	 * @param $key;关键字
	 * @return string
	 */
	public function key_reply($key)
	{
		$key_id = 0;
		//完全匹配
		$sql = "SELECT id  FROM ".$this->table('wxkeyword')." WHERE keyword='".$key."'";
		$key_id = $this->getOne($sql);
		if(empty($key_id))
		{
		   	$sql = "SELECT id, keyword FROM ".$this->table('wxkeyword')." WHERE type=0";//模糊匹配
		  	$key_list = $this->getAll($sql);
		   	foreach($key_list as $k => $v)
		   	{
			   	if(strstr($v['keyword'], $key))
			  	{
					$key_id = $v['id'];
			   	}
		   	}
		}
		
		if(!empty($key_id))
		{ 
		   	$sql="SELECT b.type, b.id FROM ".$this->table('wxkeyword')." as a,".$this->table('rule')." as b WHERE b.id=a.rid AND a.status=1 AND a.id='".$key_id."'";
		   	$keyword = $this->getRow($sql);
		   
		   	$result = '';
		   	if($keyword['type'] == 1)
		   	{
		   		$sql = "SELECT content FROM ".$this->table('basic_reply')." WHERE rid='".$keyword['id']."'";
		   		$result = $this->getOne($sql);
		   	}
		   	else
		   	{
		   		$sql = "SELECT title, description, thumb, url FROM ".$this->table('news_reply')." WHERE rid='".$keyword['id']."'";
		   		$result = $this->getAll($sql);
		   	}
		}      
		return $result;
	}
	
	/**
	 * 获取微信规则
	 * @param $id
	 * @return string
	 */
	public function get_msg($id)
	{
		$sql = "SELECT type FROM ".$this->table('rule')." WHERE id='".$id."'";
		$type = $this->getOne($sql);
		$result = '';
		
		if($type == 1)
		{
			$sql = "SELECT content FROM ".$this->table('basic_reply')." WHERE rid='".$id."'";
			$result = $this->getOne($sql);
		}
		else 
		{
			$sql = "SELECT title,description,thumb,url FROM ".$this->table('news_reply')." WHERE rid='".$id."'";
			$result = $this->getAll($sql);
		}
		return $result;
	}
    
    /**
     * 获取绑定的token名称
     * @return unknown
     */
	public function getToken()
	{
		$sql="SELECT op_value FROM ".$this->table('options')." WHERE op_key='token'";
		$is_vaild=$this->getOne($sql);
		return  $is_vaild;
	}
    
    /**
     * 自动回复
     * @return string
     */
    public function get_dreply()
    {
    	$sql = "SELECT op_value FROM ".$this->table('options')." WHERE op_key='wxdreply'";
    	$areply_id = $this->getOne($sql);
    	 
    	$areply_content = '';
    	if($areply_id)
    	{
    		$sql = "SELECT type FROM ".$this->table('rule')." WHERE id='".$areply_id."'";
    		$rule_type = $this->getOne($sql);
    		if($rule_type == 1)
    		{
    			$sql = "SELECT content FROM ".$this->table('basic_reply')." WHERE rid='".$areply_id."'";
    			$areply_content = $this->getOne($sql);
    		}
    		elseif($rule_type == 2)
    		{
    			$sql = "SELECT * FROM ".$this->table('news_reply')." WHERE rid='".$areply_id."'";
    			$areply_content = $this->getAll($sql);
    		}
    	}
    	if(empty($areply_content))
    	{
    		$areply_content = "您输入的关键字不存在,该公众号没有配置默认回复";
    	}
    	return $areply_content;
    }
    
    /**
     * 关注是回复
     * @return string
     */
    public function get_areply()
    {
    	$sql = "SELECT op_value FROM ".$this->table('options')." WHERE op_key='wxareply'";
    	$areply_id = $this->getOne($sql);
    	
    	$areply_content = '';
    	if($areply_id)
    	{
    		$sql = "SELECT type FROM ".$this->table('rule')." WHERE id='".$areply_id."'";
    		$rule_type = $this->getOne($sql);
    		if($rule_type == 1)
    		{
    			$sql = "SELECT content FROM ".$this->table('basic_reply')." WHERE rid='".$areply_id."'";
    			$areply_content = $this->getOne($sql);
    		}
    		elseif($rule_type == 2)
    		{
    			$sql = "SELECT * FROM ".$this->table('news_reply')." WHERE rid='".$areply_id."'";
    			$areply_content = $this->getAll($sql);
    		}
    	}
    	if(empty($areply_content))
    	{
    		$areply_content = "欢迎关注公众号";
    	}
    	return $areply_content;
    }
    
    function save_user($openid)
    {
    	$sql="SELECT count(*) FROM ".$this->table('wxusers')." WHERE openid='".$openid."'";
    	$user_num=$this->getOne($sql);
    	
    	if($user_num>0)
    	{
    		$sql="UPDATE ".$this->table('wxusers')." SET is_follow='1'  WHERE openid='".$openid."'";
    		$user_num=$this->query($sql);
    	}
    	else 
    	{
    		$sql="INSERT INTO ".$this->table('wxusers')."(openid,createtime) VALUES('".$openid."','".$_SERVER['REQUEST_TIME']."')";
    		
    		
    		$user_num=$this->query($sql);
    	}
    }
    
    function cancel_follow($openid)
    {
    	$sql="UPDATE ".$this->table('wxusers')." SET is_follow='0'  WHERE openid='".$openid."'";
    	$user_num=$this->query($sql);
    }
    
	
	
}